<?php
include 'PublicdetailController.php';
include ("library/PHPExcel/PHPEXCHelper.php");
include 'HelpFuncExportExcel.php';
/**
 * PHPExcel
 */
require_once 'library/PHPExcel/PHPExcel.php';
/**
 * PHPExcel_IOFactory
 */
require_once 'library/PHPExcel/PHPExcel/IOFactory.php';
/**
 *
 * @author Daitk
 *        
 */
class BaocaoController extends PublicdetailController {
	private $_MBaocao;
	public function init() {
		$this->initValue ();
		$this->_MBaocao = new Model_Baocao ();
	}
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/index/login' );
			}
		}
	}
	/**
	 * - Ngân sách trạm y tế
	 */
	  public function indexAction() {
	  $this->view->itemcs = @$_REQUEST['id'];
	  }
	public function expxlsnstytAction() {
		$this->_helper->layout ()->disableLayout ();
		$helpExport = new HelpFuncExportExcel ();
		$objReader = PHPExcel_IOFactory::createReader ( "Excel5" );
		$colIndex = '';
		$rowIndex = 0;
		try {
			$objPHPExcel = new PHPExcel ();
			$sheet = $objPHPExcel->getActiveSheet ();
			/*
			 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá trị
			 * tĩnh.
			 */
			    $id = $this->_getParam ( 'id' );
				 if($id){
				 $jsonObj = $this->_MBaocao->getdanhsachuserct($id);
				 }
				 else
				 {
				$jsonObj = $this->_MBaocao->getdanhsachuser();
				 }
				
				/*
				 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá
				 * trị tĩnh.
				 */
				$sheet->setCellValue ( 'A2', 'Khách sạn Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 2, 2, 2 );
				$sheet->setCellValue ( 'A3', 'Công ty Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 3, 2, 3 );
				$sheet->setCellValue ( 'D5', 'Danh sách bảng lương' );
				$sheet->mergeCellsByColumnAndRow ( 0, 5, 6, 5 );
				$helpExport->setStyleTitleDoc ( $sheet, 'D4', 'F4' );
				
				$sheet->setCellValue ( 'E5', date ( "d/m/Y" ) );
				$sheet->mergeCellsByColumnAndRow ( 0, 6, 6, 6 );
				$helpExport->setStyleDateDoc ( $sheet, 'E5', 'F5' );
				
				$rowStart = 8;
				$colStart = 'A';
				$rowIndex = $rowStart;
				$colIndex = $colStart;
				$sheet = $objPHPExcel->getActiveSheet ();
				// BEGIN set width for column
				$sheet->getColumnDimension ( 'A' )->setWidth ( 5 );
				$sheet->getColumnDimension ( 'B' )->setWidth ( 10 );
				$sheet->getColumnDimension ( 'C' )->setWidth ( 30 );
				$sheet->getColumnDimension ( 'D' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'E' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'F' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'G' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'H' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'I' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'J' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'K' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'L' )->setWidth ( 10 );
				$sheet->getColumnDimension ( 'M' )->setWidth ( 22 );
				$sheet->getColumnDimension ( 'N' )->setWidth ( 10 );
				// END set width for column
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'STT', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Mã NV', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Họ Tên', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Chức Vụ', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Số điện thoai', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Ngày nghỉ', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Trợ cấp', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tạm ứng', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Lương cơ bản', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Thực lĩnh', $colIndex );
				//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Kê Khai', $colIndex );
				
				$helpExport->setStyleTitleTable ( $sheet, $colStart . $rowStart, $colIndex . $rowIndex );
				
				foreach ( $jsonObj as $row ) {
					$rowIndex += 1;
					$colIndex = $colStart;
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $rowIndex - $rowStart, $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['id'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['name'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['roles'], $colIndex );
					//$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['email'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['phone'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['ngaynghi'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['trocap'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['tamung'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['luong'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['thuclinh'], $colIndex );
					
					
						//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Nhập tài sản', $colIndex );
				}
				// BEGIN row total
				/*$rowIndex += 1;
				$sheet->setCellValue ( 'A' . $rowIndex, 'Tổng số sô lương (VNĐ) : ' . $jsonObj [0] ['luong'] );
				$sheet->mergeCellsByColumnAndRow ( 0, $rowIndex, 4, $rowIndex );
				$sheet->setCellValue ( 'F' . $rowIndex, 'Tổng tiền thực trả (VNĐ): ' . $jsonObj [0] ['luong'] );
				$sheet->mergeCellsByColumnAndRow ( 5, $rowIndex, 9, $rowIndex );
				$helpExport->setStyleSummaryTable ( $sheet, 'A' . $rowIndex, 'J' . $rowIndex );
				// END row toal
				$sheet->getStyle ( $colStart . $rowStart . ':' . $colIndex . $rowIndex )->getBorders ()->getOutline ()->setBorderStyle ( PHPExcel_Style_Border::BORDER_THIN );
				$sheet->getStyle ( $colStart . $rowStart . ':' . $colIndex . $rowIndex )->getBorders ()->getInside ()->setBorderStyle ( PHPExcel_Style_Border::BORDER_THIN );*/
				
				$rowIndex += 2;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Người in danh sách' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$rowIndex += 1;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Ký tên' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$helpExport->setStyleTitleTable ( $sheet, 'H' . ($rowIndex - 1), 'H' . $rowIndex );
				$objPHPExcel->getActiveSheet ()->setTitle ( 'Danh sách bảng lương' );
				/*
				 * Set active sheet index to the first sheet, so Excel opens
				 * this as the first sheet
				 */
				$objPHPExcel->setActiveSheetIndex ( 0 );
				/* Redirect output to a clientâ€™s web browser (Excel5) */
				
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="Danh_sach_bang_luong(' . date ( "d/m/Y" ) . ').xls"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			
		
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
					alert('Exception: Export Excel!');
					</script>";
		}
	}
	
	public function danhsachnoAction() {
		$this->_helper->layout ()->disableLayout ();
		$helpExport = new HelpFuncExportExcel ();
		$objReader = PHPExcel_IOFactory::createReader ( "Excel5" );
		$colIndex = '';
		$rowIndex = 0;
		try {
			$objPHPExcel = new PHPExcel ();
			$sheet = $objPHPExcel->getActiveSheet ();
			/*
			 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá trị
			 * tĩnh.
			 */
			     $cmt = $this->_getParam ( 'cmt' );
				 $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	             $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
				 if($cmt){
				$jsonObj = $this->_MBaocao->getdanhsachnoct ($cmt);
				$hd = 'Danh sách hóa đơn nợ';
				 }
				 else
				 {
				$jsonObj = $this->_MBaocao->getdanhsachno ();
				$hd = 'Danh sách hóa đơn nợ';
				 }
				 if($startday != '' && $endday != ''){
	              $jsonObj = $this->_MBaocao->getorders($startday,$endday);
				  $hd = 'Danh sách hóa đơn nợ';
				 }
				/*
				 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá
				 * trị tĩnh.
				 */
				$sheet->setCellValue ( 'A2', 'Khách sạn Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 2, 2, 2 );
				$sheet->setCellValue ( 'A3', 'Công ty Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 3, 2, 3 );
				$sheet->setCellValue ( 'D5', $hd);
				$sheet->mergeCellsByColumnAndRow ( 0, 5, 6, 5 );
				$helpExport->setStyleTitleDoc ( $sheet, 'D4', 'F4' );
				
				$sheet->setCellValue ( 'E5', date ( "d/m/Y" ) );
				$sheet->mergeCellsByColumnAndRow ( 0, 6, 6, 6 );
				$helpExport->setStyleDateDoc ( $sheet, 'E5', 'F5' );
				
				$rowStart = 8;
				$colStart = 'A';
				$rowIndex = $rowStart;
				$colIndex = $colStart;
				$sheet = $objPHPExcel->getActiveSheet ();
				// BEGIN set width for column
				$sheet->getColumnDimension ( 'A' )->setWidth ( 5 );
				$sheet->getColumnDimension ( 'B' )->setWidth ( 10 );
				$sheet->getColumnDimension ( 'C' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'D' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'E' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'F' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'G' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'H' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'I' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'J' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'K' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'L' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'M' )->setWidth ( 22 );
				$sheet->getColumnDimension ( 'N' )->setWidth ( 10 );
				// END set width for column
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'STT', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Mã HĐ', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Ngày hóa đơn', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Người lập hóa đơn', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Họ Tên', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'CMT', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Trả trước', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Khuyến mại', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Khoản khác', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Còn lại', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Nơ/Trả hết', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tổng tiền của đơn hàng', $colIndex );
				//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Kê Khai', $colIndex );
				
				$helpExport->setStyleTitleTable ( $sheet, $colStart . $rowStart, $colIndex . $rowIndex );
				
				foreach ( $jsonObj as $row ) {
					$rowIndex += 1;
					$colIndex = $colStart;
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $rowIndex - $rowStart, $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['id'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['created'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['createdBy'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['customerName'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['customerPassport'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['prepaid'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['promotion'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['fee'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['total'], $colIndex );
					if($row['debit']==1){
					  $no = "Khách nợ";
					}
					else{
					$no = "Khách trả đủ ";
					}
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $no, $colIndex );
					 $tongtien = $row['total'] + $row ['prepaid'] + $row ['fee'];
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $tongtien, $colIndex );
						//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Nhập tài sản', $colIndex );
				}
				// BEGIN row total
				
				
				$rowIndex += 2;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Người in danh sách' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$rowIndex += 1;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Ký tên' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$helpExport->setStyleTitleTable ( $sheet, 'H' . ($rowIndex - 1), 'H' . $rowIndex );
				$objPHPExcel->getActiveSheet ()->setTitle ( 'Danh sách nợ' );
				/*
				 * Set active sheet index to the first sheet, so Excel opens
				 * this as the first sheet
				 */
				$objPHPExcel->setActiveSheetIndex ( 0 );
				/* Redirect output to a clientâ€™s web browser (Excel5) */
				
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="Danh_sach_no(' . date ( "d/m/Y" ) . ').xls"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			
		
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
					alert('Exception: Export Excel!');
					</script>";
		}
	}
	
	
	public function orderAction() {
	 $baocaodoanhthu = array();
	 if($this->_getParam ( 'startday' ) != '' || $this->_getParam ( 'endday' ) != '' ){
	 $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	 $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
	 }
	 else{
	  $endday = date('Y-m-d h:i:s');
	  $startday  = date('Y-m-d h:i:s', strtotime('last day last month'));
	 }
	 $jsonObj = $this->_MBaocao->getbaocaodoanhthu($startday,$endday);
	 $jsonObjc = $this->_MBaocao->getdoanhchi($startday,$endday);
	 $baocaodoanhthu = array(
	     array("tongchi"=>$jsonObjc[0]['tongchi'],
          "tongthu"=>$jsonObj[0]['tongthu'],
			"laixuat"=>$jsonObj[0]['tongthu']-$jsonObjc[0]['tongchi'],
			"tu"=>$startday,
			"den"=>$endday)
	 );
	echo json_encode($baocaodoanhthu);
	 die;
	}
	
	public function nhapkhossAction() {
	    $page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'productName';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
	 if($this->_getParam ( 'startday' ) != '' || $this->_getParam ( 'endday' ) != '' ){
	 $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	 $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
	 }
	 else{
	  $endday = date('Y-m-d h:i:s');
	  $startday  = date('Y-m-d h:i:s', strtotime('last day last month'));
	 }
	 $jsonObj = $this->_MBaocao->getbaocaonhapkhos($startday,$endday,$sort, $order, $offset, $rows);
	echo json_encode($jsonObj);
	 die;
	}
	public function xuatkhossAction() {
	    $page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'productName';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
	 if($this->_getParam ( 'startday' ) != '' || $this->_getParam ( 'endday' ) != '' ){
	 $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	 $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
	 }
	 else{
	  $endday = date('Y-m-d h:i:s');
	  $startday  = date('Y-m-d h:i:s', strtotime('last day last month'));
	 }
	 $jsonObj = $this->_MBaocao->getbaocaoxuatkhos($startday,$endday,$sort, $order, $offset, $rows);
	echo json_encode($jsonObj);
	 die;
	}
	
	
	
	public function nhapkhoAction() {
		$this->_helper->layout ()->disableLayout ();
		$helpExport = new HelpFuncExportExcel ();
		$objReader = PHPExcel_IOFactory::createReader ( "Excel5" );
		$colIndex = '';
		$rowIndex = 0;
		try {
			$objPHPExcel = new PHPExcel ();
			$sheet = $objPHPExcel->getActiveSheet ();
			/*
			 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá trị
			 * tĩnh.
			 */
			  $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	          $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
			    $jsonObj = $this->_MBaocao->getbaocaonhapkho($startday,$endday);
				/*
				 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá
				 * trị tĩnh.
				 */
				$sheet->setCellValue ( 'A2', 'Khách sạn Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 2, 2, 2 );
				$sheet->setCellValue ( 'A3', 'Công ty Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 3, 2, 3 );
				$sheet->setCellValue ( 'D5', 'Báo cáo xuất kho' );
				$sheet->mergeCellsByColumnAndRow ( 0, 5, 6, 5 );
				$helpExport->setStyleTitleDoc ( $sheet, 'D4', 'F4' );
				
				$sheet->setCellValue ( 'E5', date ( "d/m/Y" ) );
				$sheet->mergeCellsByColumnAndRow ( 0, 6, 6, 6 );
				$helpExport->setStyleDateDoc ( $sheet, 'E5', 'F5' );
				
				$rowStart = 8;
				$colStart = 'A';
				$rowIndex = $rowStart;
				$colIndex = $colStart;
				$sheet = $objPHPExcel->getActiveSheet ();
				// BEGIN set width for column
				$sheet->getColumnDimension ( 'A' )->setWidth ( 5 );
				$sheet->getColumnDimension ( 'B' )->setWidth ( 10 );
				$sheet->getColumnDimension ( 'C' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'D' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'E' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'F' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'G' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'H' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'I' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'J' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'K' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'L' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'M' )->setWidth ( 22 );
				$sheet->getColumnDimension ( 'N' )->setWidth ( 10 );
				// END set width for column
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'STT', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Mã ĐH nhập', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Ngày Nhập', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Người Nhập', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tên hàng nhập', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Số lượng nhập', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Số tiền/1 đơn vị', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tổng tiền/ mặt hàng', $colIndex );
				//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Kê Khai', $colIndex );
				
				$helpExport->setStyleTitleTable ( $sheet, $colStart . $rowStart, $colIndex . $rowIndex );
				
				foreach ( $jsonObj as $row ) {
					$rowIndex += 1;
					$colIndex = $colStart;
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $rowIndex - $rowStart, $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['id'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['created'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['username'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['productName'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['quantity'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['price'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['total'], $colIndex );
						//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Nhập tài sản', $colIndex );
				}
				// BEGIN row total
				
				
				$rowIndex += 2;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Người in danh sách' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$rowIndex += 1;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Ký tên' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$helpExport->setStyleTitleTable ( $sheet, 'H' . ($rowIndex - 1), 'H' . $rowIndex );
				$objPHPExcel->getActiveSheet ()->setTitle ( 'Danh sách nợ' );
				/*
				 * Set active sheet index to the first sheet, so Excel opens
				 * this as the first sheet
				 */
				$objPHPExcel->setActiveSheetIndex ( 0 );
				/* Redirect output to a clientâ€™s web browser (Excel5) */
				
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="Danh_sach_nhap(' . date ( "d/m/Y" ) . ').xls"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			
		
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
					alert('Exception: Export Excel!');
					</script>";
		}
	}
	
	
	
	public function xuatkhoAction() {
		$this->_helper->layout ()->disableLayout ();
		$helpExport = new HelpFuncExportExcel ();
		$objReader = PHPExcel_IOFactory::createReader ( "Excel5" );
		$colIndex = '';
		$rowIndex = 0;
		try {
			$objPHPExcel = new PHPExcel ();
			$sheet = $objPHPExcel->getActiveSheet ();
			/*
			 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá trị
			 * tĩnh.
			 */
			  $startday =   date('Y-m-d', strtotime($this->_getParam ( 'startday' )));
	          $endday =   date('Y-m-d', strtotime($this->_getParam ( 'endday' )));
			  $jsonObj = $this->_MBaocao->getbaocaoxuatkho($startday,$endday);
				/*
				 * Bắt đầu set các giá trị, căn chỉnh style Bắt đầu set các giá
				 * trị tĩnh.
				 */
				$sheet->setCellValue ( 'A2', 'Khách sạn Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 2, 2, 2 );
				$sheet->setCellValue ( 'A3', 'Công ty Hồng Long' );
				$sheet->mergeCellsByColumnAndRow ( 0, 3, 2, 3 );
				$sheet->setCellValue ( 'D5', 'Báo cáo kho' );
				$sheet->mergeCellsByColumnAndRow ( 0, 5, 6, 5 );
				$helpExport->setStyleTitleDoc ( $sheet, 'D4', 'F4' );
				
				$sheet->setCellValue ( 'E5', date ( "d/m/Y" ) );
				$sheet->mergeCellsByColumnAndRow ( 0, 6, 6, 6 );
				$helpExport->setStyleDateDoc ( $sheet, 'E5', 'F5' );
				
				$rowStart = 8;
				$colStart = 'A';
				$rowIndex = $rowStart;
				$colIndex = $colStart;
				$sheet = $objPHPExcel->getActiveSheet ();
				// BEGIN set width for column
				$sheet->getColumnDimension ( 'A' )->setWidth ( 5 );
				$sheet->getColumnDimension ( 'B' )->setWidth ( 10 );
				$sheet->getColumnDimension ( 'C' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'D' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'E' )->setWidth ( 25 );
				$sheet->getColumnDimension ( 'F' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'G' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'H' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'I' )->setWidth ( 12 );
				$sheet->getColumnDimension ( 'J' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'K' )->setWidth ( 15 );
				$sheet->getColumnDimension ( 'L' )->setWidth ( 20 );
				$sheet->getColumnDimension ( 'M' )->setWidth ( 22 );
				$sheet->getColumnDimension ( 'N' )->setWidth ( 10 );
				// END set width for column
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'STT', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Mã xuất', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Ngày xuất', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Người xuất', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tên hàng xuất', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Số lượng xuất', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Số tiền/1 đơn vị', $colIndex );
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Tổng tiền/ mặt hàng', $colIndex );
				//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Kê Khai', $colIndex );
				
				$helpExport->setStyleTitleTable ( $sheet, $colStart . $rowStart, $colIndex . $rowIndex );
				
				foreach ( $jsonObj as $row ) {
					$rowIndex += 1;
					$colIndex = $colStart;
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $rowIndex - $rowStart, $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['id'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['created'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['createdBy'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['productName'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['quantity'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['price'], $colIndex );
					$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, $row ['totals'], $colIndex );
						//$helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, 'Nhập tài sản', $colIndex );
				}
				// BEGIN row total
				
				
				$rowIndex += 2;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Người in danh sách' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$rowIndex += 1;
				$sheet->setCellValue ( 'H' . $rowIndex, 'Ký tên' );
				$sheet->mergeCellsByColumnAndRow ( 7, $rowIndex, 9, $rowIndex );
				$helpExport->setStyleTitleTable ( $sheet, 'H' . ($rowIndex - 1), 'H' . $rowIndex );
				$objPHPExcel->getActiveSheet ()->setTitle ( 'Danh sách nợ' );
				/*
				 * Set active sheet index to the first sheet, so Excel opens
				 * this as the first sheet
				 */
				$objPHPExcel->setActiveSheetIndex ( 0 );
				/* Redirect output to a clientâ€™s web browser (Excel5) */
				
				header ( 'Content-Type: application/vnd.ms-excel' );
				header ( 'Content-Disposition: attachment;filename="Danh_sach_xuat(' . date ( "d/m/Y" ) . ').xls"' );
				header ( 'Cache-Control: max-age=0' );
				$objWriter = PHPExcel_IOFactory::createWriter ( $objPHPExcel, 'Excel5' );
				$objWriter->save ( 'php://output' );
			
		
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
					alert('Exception: Export Excel!');
					</script>";
		}
	}
	
	
	/**
	 * - Khởi tạo các dòng trong sheet Excel với dữ liệu được đưa vào.
	 * 
	 * @param PHPExcel_Worksheet $_sheet        	
	 * @param HelpFuncExportExcel $_helpExport        	
	 * @param Array $_data        	
	 * @param Number $_rowIndex        	
	 * @param Number $_numCol        	
	 * @return Number
	 */
	public function initRow($_sheet, $_helpExport, $_data, $_rowIndex, $_numCol) {
		$helpExport = $_helpExport;
		$sheet = $_sheet;
		$rowIndex = $_rowIndex;
		$data = $_data;
		$numCol = $_numCol;
		foreach ( $data as $item ) {
			$rowIndex += 1;
			$colIndex = 'A';
			for($i = 0; $i < $numCol; $i ++) {
				$colIndex = $helpExport->setValueForSheet ( $sheet, $colIndex . $rowIndex, (isset ( $item [$i] ) ? $item [$i] : ""), $colIndex );
			}
		}
		return $rowIndex;
	}
	
}








